package com.superitem.school.service;

import com.superitem.school.mapper.StudentMapper;
import com.superitem.school.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;

    public List<Student> showAllStudent(){
        List<Student> students = studentMapper.selectAll();
        return students;
    }

    public Student selectById(Integer id){
        return studentMapper.selectByPrimaryKey(id);
    }

    public Student updateStuName(Integer id, String name){
        Student student = studentMapper.selectByPrimaryKey(id);
        student.setStuName(name);
        studentMapper.updateByPrimaryKey(student);
        return student;
    }

    public Student updateStuAge(Integer id, Integer age){
        Student student = studentMapper.selectByPrimaryKey(id);
        student.setStuAge(age);
        studentMapper.updateByPrimaryKey(student);
        return student;
    }

    public int updateStu(Student student){
        return studentMapper.updateByPrimaryKey(student);
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

}
